package com.yanqu.road.server.cmd.banquet.party;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.banquet.UserBanquetData;
import com.yanqu.road.entity.banquet.invitation.BanquetChildInvitation;
import com.yanqu.road.entity.banquet.invitation.BanquetEstateInvitation;
import com.yanqu.road.entity.banquet.invitation.BanquetMarryInvitation;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.server.pb.BanquetPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_CROSS_PARTY_REWARD, desc = "")
public class CrossPartyGetRewardResultCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BanquetProto.CrossPartyGetRewardRespMsg msg = BanquetProto.CrossPartyGetRewardRespMsg.parseFrom(packet.getBytes());
        ret = msg.getRet();
        BanquetProto.BanquetGetRewardResp.Builder resp = BanquetProto.BanquetGetRewardResp.newBuilder();
        resp.setRet(ret);
        if (ret == 0) {
            int fillRobotNum = msg.getFillRobotNum();
            player.getModule(BanquetModule.class).addBanquetNormalNum(fillRobotNum);
            List<BanquetProto.BanquetGetRewardTempMsg> rewardListList = msg.getRewardListList();
            for (BanquetProto.BanquetGetRewardTempMsg rewardTempMsg : rewardListList) {
                BanquetProto.BanquetGetRewardTempMsg.Builder tempMsg = rewardTempMsg.toBuilder();
                long popularity = tempMsg.getPopularity();
                long integral = tempMsg.getIntegral();

                CrossBanquetMgr.addBanquetPopularityAndIntegral(player,popularity,integral,eLogMoneyType.BanquetOpenEndReward.getValue());
                //婚宴增加亲密度
                if (tempMsg.getType() == eBanquetInfoType.MarryBanquet.getValue()) {
                    BanquetMarryInvitation inv = (BanquetMarryInvitation) getInvitation(tempMsg.getType(), tempMsg.getInfo());
                    int addIntimacyValue = BanquetConfigMgr.getBeautyAddValue(tempMsg.getType());
                    UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(inv.getBeautyId());
                    player.getModule(BeautyModule.class).addIntimacy(userBeauty, addIntimacyValue, eLogMoneyType.Beauty.getValue(), eLogMoneyType.BeautyInvitation.getValue());
                    player.getModule(BeautyModule.class).syncUserBeauty(userBeauty);
                } else if (tempMsg.getType() == eBanquetInfoType.ChildBanquet.getValue()) {//状元宴增加孩子赚速
                    BanquetChildInvitation inv = (BanquetChildInvitation) getInvitation(tempMsg.getType(), tempMsg.getInfo());
                    int addEarnSpeedRatio = BanquetConfigMgr.getChildAddRatio(tempMsg.getType());
                    String earnSpeed = MarriageMgr.childBanquetAddEarnSpeed(player, inv.getChildUid(), addEarnSpeedRatio);
                    log.info("user {} banquet {} add earnSpeed {}", player.getUserId(), tempMsg.getBanquetId(), earnSpeed);
                    tempMsg.setChildEarnSpeed(earnSpeed);
                }
                resp.addRewardList(tempMsg);
            }
        }

        Map<String, UserBanquetData> userBanquetDataMap = BanquetMgr.getOverBanquetReward(player);
        BanquetProto.BanquetGetRewardResp.Builder respMsg = BanquetPb.parseBanquetGetRewardResp(userBanquetDataMap);
        for (BanquetProto.BanquetGetRewardTempMsg.Builder builder : respMsg.getRewardListBuilderList()) {
            resp.addRewardList(builder);
        }

        player.sendPacket(Protocol.U_BANQUET_GET_REWARD, resp);
    }


    public Object getInvitation(int type, String invitationInfo) {
        if (type == eBanquetInfoType.MarryBanquet.getValue()) {
            return JSON.parseObject(invitationInfo, BanquetMarryInvitation.class);
        } else if (type == eBanquetInfoType.ChildBanquet.getValue()) {
            return JSON.parseObject(invitationInfo, BanquetChildInvitation.class);
        } else if (type == eBanquetInfoType.EstateBanquet.getValue()) {
            return JSON.parseObject(invitationInfo, BanquetEstateInvitation.class);
        }
        return null;
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
